package com.sh.shanhuo.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;


import com.sh.shanhuo.R;

import androidx.fragment.app.Fragment;


/***
 *
 * 界面结束动画类
 */
public class ScreenSwitch {

    public static final int REGISTER_SUCCESS = 0x00;
    public static final int RESET_PASSWORD = 0x01;
    public static void switchActivity0(Context context, Class<?> descClass,
                                       Class<?> class1) {
        if (context == null) {
            return;
        }
        Class<?> mClass = context.getClass();
        if (mClass == descClass) {
            return;
        }
        try {
            Intent intent = new Intent();
            intent.setClass(context, descClass);
            intent.putExtra("activity", class1);
            ((Activity) context).startActivityForResult(intent, 0);
            ((Activity) context).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        } catch (Exception e) {
        }
    }

    /**
     * 启动界面
     *
     * @param context
     * @param descClass
     * @param bundle
     */
    public static void switchActivity(Context context, Class<?> descClass,
                                      Bundle bundle) {
        if (context == null) {
            return;
        }
        Class<?> mClass = context.getClass();
        if (mClass == descClass) {
            return;
        }
        try {
            Intent intent = new Intent();
            intent.setClass(context, descClass);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            ((Activity) context).startActivity(intent);
            ((Activity) context).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        } catch (Exception e) {
            Log.e("sql", "跳转界面失败 e=" + e.toString());
        }
    }

    /**
     * 启动界面
     *
     * @param context
     * @param descClass
     * @param bundle
     * @param requestCode
     */
    public static void switchActivity(Context context, Class<?> descClass,
                                      Bundle bundle, int requestCode) {
        if (context == null) {
            return;
        }
        Class<?> mClass = context.getClass();
        if (mClass == descClass) {
            return;
        }
        try {
            Intent intent = new Intent();
            intent.setClass(context, descClass);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            ((Activity) context).startActivityForResult(intent, requestCode);
            ((Activity) context).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        } catch (Exception e) {
            Log.e("sql", "跳转界面失败 e=" + e.toString());
        }
    }


    /**
     * 跳转activity有动画（overridePendingTransition），再调用finish时，anim要传入fase，
     * 否则finish的动画（overridePendingTransition）会覆盖掉跳转activity的动画
     */
    public static void finish(Activity context, boolean anim) {
        if (context == null) {
            return;
        }
        try {
            // 有些没有弹出键盘的会抛异
            InputMethodManager manager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (manager.isActive() && context.getCurrentFocus() != null) {
                manager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        context.finish();
        if (anim) {
            context.overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
        }
    }

    public static void finish(Activity context) {
        finish(context, true);
    }

    public static void finish(Activity context, Intent intent, int resultCode, boolean anim) {
        if (context == null) {
            return;
        }
        try {
            // 有些没有弹出键盘的会抛异
            InputMethodManager manager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (manager.isActive() && context.getCurrentFocus() != null) {
                manager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        context.setResult(resultCode, intent);
        context.finish();
        if (anim) {
            context.overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
        }
    }

    public static void finish(Activity context, Intent intent, int resultCode) {
        finish(context, intent, resultCode, true);
    }

    /**
     * 启动界面
     *
     * @param frag
     * @param descClass
     * @param bundle
     * @param requestCode
     */
    public static void switchActivity(Fragment frag, Class<?> descClass, Intent bundle, int requestCode) {
        Activity act = frag.getActivity();
        Class<?> mClass = act.getClass();
        if (mClass == descClass) {
            return;
        }
        try {

            Intent intent = new Intent();
            intent.setClass(act, descClass);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            frag.startActivityForResult(intent, requestCode);
            act.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        } catch (Exception e) {
            Log.e("sql", "跳转界面失败 e=" + e.toString());
        }
    }


    /**
     * 启动界面
     *
     * @param context
     * @param descClass
     * @param bundle
     */
    public static void startActivity(Context context, Class<?> descClass,
                                     Bundle bundle) {
        if (context == null) {
            return;
        }
        Class<?> mClass = context.getClass();
        if (mClass == descClass) {
            return;
        }
        try {
            Intent intent = new Intent();
            intent.setClass(context, descClass);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            ((Activity) context).startActivity(intent);
            ((Activity) context).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        } catch (Exception e) {
            Log.e("sql", "跳转界面失败 e=" + e.toString());
        }
    }


    /**
     * 启动界面
     *
     * @param context
     * @param descClass
     * @param bundle
     */
    public static void startActivity(Context context, Class<?> descClass,
                                     Bundle bundle, int requestCode) {
        if (context == null) {
            return;
        }
        Class<?> mClass = context.getClass();
        if (mClass == descClass) {
            return;
        }
        try {
            Intent intent = new Intent();
            intent.setClass(context, descClass);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            ((Activity) context).startActivityForResult(intent, requestCode);
            ((Activity) context).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        } catch (Exception e) {
            Log.e("sql", "跳转界面失败 e=" + e.toString());
        }
    }

    public static void finishNormal(Activity context) {
        if (context == null) {
            return;
        }
        try {
            // 有些没有弹出键盘的会抛异�?by yaoc
            InputMethodManager manager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (manager.isActive() && context.getCurrentFocus() != null) {
                manager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        context.finish();
        context.overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    public static void finishNormal(Activity context, Intent intent, int resultCode) {
        if (context == null) {
            return;
        }
        try {
            // 有些没有弹出键盘的会抛异�?by yaoc
            InputMethodManager manager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (manager.isActive() && context.getCurrentFocus() != null) {
                manager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        context.setResult(resultCode, intent);
        context.finish();
        context.overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }
}
